<?php

namespace app\common\dao\info;

use app\common\model\info\Info as model;
use app\common\dao\BaseDao;

class InfoDao extends BaseDao
{
    /**
    * 设置模型
    * @return model
    */
    protected function getModel(): string
    {
        return model::class;
    }

    public function search($where)
    {
        $query = $this->getModel()::getDB()->alias('i')->leftJoin('user u','u.uid = i.uid');
        $query->when(isset($where['city_id']) && $where['city_id'] !== '', function ($query)use($where){
            $query->where('i.city_id',$where['city_id']);
        })->when(isset($where['cate']) && $where['cate'] !== '', function ($query)use($where){
            $query->where('i.cate','like',"%{$where['cate']}%");
        })->when(isset($where['type']) && $where['type'] !== '', function ($query)use($where){
            if(is_array($where['type'])){
                $query->whereIn('i.type',$where['type']);
            }else{
                $query->where('i.type',$where['type']);
            }
        })->when(isset($where['status']) && $where['status'] !== '', function ($query)use($where){
            if(is_array($where['status'])){
                $query->whereIn('i.status',$where['status']);
            }else{
                $query->where('i.status',$where['status']);
            }
        })->when(isset($where['start_time']) && $where['start_time'] !== '', function ($query)use($where){
            $query->where('i.create_time','>',$where['start_time']);
        })->when(isset($where['end_time']) && $where['end_time'] !== '', function ($query)use($where){
            $query->where('i.create_time','<',$where['end_time']);
        })->when(isset($where['uid']) && $where['uid'] !== '', function ($query)use($where){
            $query->where('i.uid',$where['uid']);
        })->when(isset($where['lawyer_id']) && $where['lawyer_id'] !== '', function ($query)use($where){
            $query->where('i.lawyer_id',$where['lawyer_id']);
        })->when(isset($where['nickname']) && $where['nickname'] !== '', function ($query)use($where){
            $query->where('u.nickname','like',"%{$where['nickname']}%");
        })->when(isset($where['phone']) && $where['phone'] !== '', function ($query)use($where){
            $query->where('u.phone','like',"%{$where['phone']}%");
        })->field('i.*');

        return $query->where('i.status','<>',0);
    }

}